package br.edu.curso.java.refatorar;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;


public abstract class PessoaFisicaDAO<t extends PessoaFisicaVO> extends BaseDAOChaveUnica<t, Integer> {
    
	
	protected t carregarObjeto(t obj, ResultSet rs) {
		
		
		try {
			obj.setCodigo(rs.getInt("codigo"));
			obj.setNome(rs.getString("nome"));
			obj.setEndereco(rs.getString("endereco"));
			obj.setBairro(rs.getString("bairro"));
			obj.setCidade(rs.getString("cidade"));
			obj.setUf(rs.getString("uf"));
			obj.setTelefone(rs.getString("telefone"));
			obj.setCelular(rs.getString("celular"));
			obj.setCpf(rs.getString("cpf"));
			obj.setDtNascimento(new Date(rs.getDate("dtnascimento").getTime()));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return obj;
	}

	@Override
	protected int carregarCampos(PreparedStatement pstm,	t obj, int i) {

		try {
				
			pstm.setString(i, obj.getNome());		
                        pstm.setString(++i, obj.getEndereco());
			pstm.setString(++i, obj.getBairro());
                        pstm.setString(++i, obj.getCidade());
                        pstm.setString(++i, obj.getUf());
                        pstm.setString(++i, obj.getTelefone());
			pstm.setString(++i, obj.getCelular());
			pstm.setString(++i, obj.getCpf());
			pstm.setDate( ++i,new java.sql.Date( obj.getDtNascimento().getTime()));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}

	@Override
	public String getChavePrimaria() {
		return "codigo";
	}


	@Override
	protected PreparedStatement carregarChavePrimaria(PreparedStatement pstm,
			Integer codigo) {
		
		try {
			
			pstm.setInt(1, codigo);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return pstm;
	}


	@Override
	protected int carregarChavePrimaria(PreparedStatement pstm,
			t obj, int i) {
		
		try {
			pstm.setInt(i, obj.getCodigo());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}

	@Override
	public abstract String getNomeTabela();


	@Override
	protected abstract t getObjeto();

         protected Integer carregarChave(ResultSet rs) {
                
            Integer i = 0;
            
            try {
                
                 i = rs.getInt(1);
             
            } catch (SQLException e) {
		e.printStackTrace();
            }
            
            return i;
            
          };
}
